package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.entity.User;
import net.wanho.service.UserService;
import net.wanho.vo.AjaxResult;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Author：汤小洋
 * Date：2025-07-03 9:45
 * Description：<描述>
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @PostMapping
    public AjaxResult add(@RequestBody User user){
        userService.save(user); // 返回主键
        return AjaxResult.success(user);
    }

    @GetMapping
    public AjaxResult list(){
        List<User> list = userService.list();
        return AjaxResult.success(list);
    }

    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable Integer id){
        userService.removeById(id);
        return AjaxResult.success("删除用户成功");
    }

    @PutMapping
    public AjaxResult modify(@RequestBody User user){
        // userService.updateById(user);
        userService.saveOrUpdate(user);
        return AjaxResult.success(user);
    }
}
